﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

namespace Infrastructure.WebMvc.ActionInvoker
{
    public class UnityActionInvoker : ControllerActionInvoker
    {
        private IUnityContainer _container;
        public UnityActionInvoker(IUnityContainer container)
        {
            this._container = container;
        }
        protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            var filters = base.GetFilters(controllerContext, actionDescriptor);
            foreach (var item in filters.AuthorizationFilters)
            {
                if (item is MvcAttributes.RuleAuthorizeAttribute)
                    _container.BuildUp<MvcAttributes.RuleAuthorizeAttribute>(item as MvcAttributes.RuleAuthorizeAttribute);                 
            }
            foreach (var item in filters.ActionFilters)
            {
                _container.BuildUp(item);
            }
            foreach (var item in filters.ResultFilters)
            {
                _container.BuildUp(item);
            }
            foreach (var item in filters.ExceptionFilters)
            {
                _container.BuildUp(item);                
            }
            return filters; 
        }
    }
}
